package org.come.control;

import java.math.BigDecimal;
import java.util.List;

import org.come.Frame.TeamApplyJframe;
import org.come.Frame.TeamCreateJframe;
import org.come.Frame.TeamJframe;
import org.come.Frame.ZhuFrame;
import org.come.action.FromServerAction;
import org.come.bean.RoleShow;
import org.come.bean.TeamBean;
import org.come.entity.TeamRole;
import org.come.model.InternalForm;
import org.come.socket.AgreementUtil;
import org.come.until.FormsManagement;
import org.come.until.GsonUtil;

import com.tool.image.ImageMixDeal;
import com.tool.image.ManimgAttribute;
import com.tool.role.RoleData;

public class TeamControl implements FromServerAction {

	@Override
	public void controlMessFromServer(String mes, String type) {
		if (type.equals(AgreementUtil.team1)) {
			TeamBean teamBean = GsonUtil.getGsonUtil().getgson().fromJson(mes, TeamBean.class);
			RoleData.getRoleData().setTeamBean(teamBean);
			// 刷新上面一排图标
			SX(ImageMixDeal.userimg.getRoleShow(), teamBean);
		} else if (type.equals(AgreementUtil.team2)) {
			TeamRole teamRole = GsonUtil.getGsonUtil().getgson().fromJson(mes, TeamRole.class);
			if (FormsManagement.getInternalForm2(30) != null) {
				if (FormsManagement.getframe(30).isVisible()) {
					TeamApplyJframe.getTeamApplyJframe().getTeamApplyJpanel().showTeamRole(teamRole);
					return;
				}
			}
			ZhuFrame.getZhuJpanel().setTeamState(1);
		} else if (type.equals(AgreementUtil.team3)) {
			team3(mes);
		} else if (type.equals(AgreementUtil.team4)) {
			team4(mes);
		} else if (type.equals(AgreementUtil.team6)) {
			TeamRoleList tBean = GsonUtil.getGsonUtil().getgson().fromJson(mes, TeamRoleList.class);
			TeamApplyJframe.getTeamApplyJframe().getTeamApplyJpanel().showTeamRoleList(tBean.t);
		} else if (type.equals(AgreementUtil.enlist)) {
			TeanBeanList tBean = GsonUtil.getGsonUtil().getgson().fromJson(mes, TeanBeanList.class);
			TeamCreateJframe.getTeamCreateJframe().getTeamCreateJpanel().initializeJlist(tBean.t);
		}
	}

	class TeamRoleList {
		private List<TeamRole> t;
	}

	class TeanBeanList {
		private List<TeamBean> t;
	}

	/** 修改队伍状态 */
	public void team4(String mes) {
		TeamBean teamBean = RoleData.getRoleData().getTeamBean();
		if (teamBean == null) {
			return;
		}
		TeamRole teamRole = GsonUtil.getGsonUtil().getgson().fromJson(mes, TeamRole.class);
		int type = 0;
		if (teamRole.getState() == -3) {
			type = 1;
		} else if (teamRole.getState() == 1 && teamBean.getTeams().get(0).getRoleId().compareTo(teamRole.getRoleId()) != 0) {
			type = 2;
		}
		if (teamBean.upTeamRole(teamRole, type)) {
			// 刷新上面一排图标
		}
		SX(ImageMixDeal.userimg.getRoleShow(), teamBean);
	}

	/** 修改队伍信息 */
	public void team3(String mes) {
		String[] v = mes.split("#");
		BigDecimal teamId = new BigDecimal(v[0]);
		if (!"null".equals(v[1])) {
			String[] vs = v[1].split("\\|");
			for (int i = 0; i < vs.length; i++) {
				ManimgAttribute attribute = ImageMixDeal.huoquLogin(vs[i]);
				if (attribute != null) {
					attribute.getRoleShow().setTroop_id(teamId);
					attribute.getRoleShow().setTeamInfo(v[1]);
					attribute.initTeam();
				}
			}
		}
		for (int i = 2; i < v.length; i++) {
			String[] sv = v[i].split("&");
			ManimgAttribute attribute = ImageMixDeal.huoquLogin(sv[0]);
			if (attribute != null) {
				int state = Integer.parseInt(sv[1]);
				if (state == -3) {
					attribute.getRoleShow().setTroop_id(null);
					if (attribute == ImageMixDeal.userimg) {
						RoleData.getRoleData().setTeamBean(null);
						SX(ImageMixDeal.userimg.getRoleShow(), null);
					}
				} else {
					attribute.getRoleShow().setTroop_id(teamId);
				}
				attribute.getRoleShow().setTeamInfo(null);
				attribute.initTeam();
			}
		}
	}

	/***/
	public void SX(RoleShow roleShow, TeamBean teamBean) {
		InternalForm internalForm = FormsManagement.getInternalForm2(13);
		if (internalForm != null) {
			if (internalForm.getFrame().isVisible()) {
				if (teamBean != null) {
					TeamJframe.getTeamJframe().getTeamjpanel().show(roleShow, teamBean);
				} else {
					FormsManagement.HideForm(13);
				}
			}
		}
		ZhuFrame.getZhuJpanel().SXTeam(roleShow, teamBean);
	}
}
